package com.mingqijia.gassafety.authority.dal.handler;

import com.mingqijia.gassafety.authority.api.req.privilege.CreatePrivilegeReqDTO;
import com.mingqijia.gassafety.authority.api.req.privilege.QueryPrivilegeReqDTO;
import com.mingqijia.gassafety.authority.api.req.privilege.UpdatePrivilegeReqDTO;
import com.mingqijia.gassafety.authority.api.resp.privilege.PrivilegeRespDTO;
import com.mingqijia.gassafety.authority.dal.command.CreatePrivilegeCommand;
import com.mingqijia.gassafety.authority.dal.command.QueryPrivilegeCommand;
import com.mingqijia.gassafety.authority.dal.command.RemovePrivilegeCommand;
import com.mingqijia.gassafety.authority.dal.command.UpdatePrivilegeCommand;
import com.mingqijia.gassafety.db.entity.auth.PrivilegeEntry;
import com.mingqijia.gassafety.shared.utils.MappingTools;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 对象转换接口
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年01月09日 20:37
 */
@Mapper(componentModel = "spring", uses = MappingTools.class)
@ConditionalOnMissingBean(PrivilegeTranslatorImpl.class)
public interface PrivilegeTranslator {

    PrivilegeRespDTO toRespDTO(PrivilegeEntry entry);

    PrivilegeEntry toEntry(CreatePrivilegeCommand command);

    @Mapping(source = "privId", target = "id")
    PrivilegeEntry toEntry(UpdatePrivilegeCommand command);

    QueryPrivilegeCommand toCommand(QueryPrivilegeReqDTO req);

    CreatePrivilegeCommand toCommand(CreatePrivilegeReqDTO req);

    UpdatePrivilegeCommand toCommand(UpdatePrivilegeReqDTO req);

    @Mapping(source = "privId", target = "id")    
    PrivilegeEntry toEntry(RemovePrivilegeCommand command);

    default List<PrivilegeRespDTO> toRespDTO(List<PrivilegeEntry> entries) {
        return Optional.ofNullable(entries)
                .orElse(Collections.emptyList())
                .stream().map(this::toRespDTO)
                .collect(Collectors.toList());
    }
}
